package org.javaforever.gatescore.reports.verb;

import java.util.ArrayList;
import java.util.List;

import org.javaforever.gatescore.core.FrontCodeBlock;
import org.javaforever.gatescore.core.FrontDomain;
import org.javaforever.gatescore.core.FrontDropdown;
import org.javaforever.gatescore.core.FrontMethod;
import org.javaforever.gatescore.core.FrontVerb;
import org.javaforever.gatescore.core.Statement;
import org.javaforever.gatescore.core.Writeable;
import org.javaforever.gatescore.core.WriteableUtil;
import org.javaforever.gatescore.utils.StringUtil;
import org.javaforever.gatescore.verb.ListActive;

public class Setup extends FrontVerb{
	private static final long serialVersionUID = 1L;
	protected FrontDomain domain;
	protected ListActiveOption listActiveOption;
	protected ListActiveSumOption listActiveSumOption;

	public Setup(FrontDomain domain,ListActiveOption listActiveOption,ListActiveSumOption listActiveSumOption) {
		super();
		this.setVerbName("Setup");
		this.setLabel("图表初始化");
		this.domain = domain;
		this.listActiveOption = listActiveOption;
		this.listActiveSumOption = listActiveSumOption;
	}

	@Override
	public FrontMethod generateControllerMethod() throws Exception {
		FrontMethod method = new FrontMethod();
		method.setStandardName("setup");
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,0,"setup() {"));
		if (this.listActiveOption.getyName() instanceof FrontDropdown) {
			ListActive la = new ListActive(((FrontDropdown)this.listActiveOption.getyName()).getTarget());
			sList.add(new Statement(2000L,1,"this."+StringUtil.lowerFirst(la.getVerbName())+"()"));
		}
		ListActive lar = new ListActive(this.domain);
		sList.add(new Statement(3000L,1,"this."+StringUtil.lowerFirst(lar.getVerbName())+"()"));
		sList.add(new Statement(4000L,1,"setTimeout(() => {"));
		sList.add(new Statement(5000L,2,"var chartstype = this.chartType"));
		sList.add(new Statement(6000L,2,"var reporttype = this.dataType"));
		sList.add(new Statement(7000L,2,"let myChart = this.$echarts.init(document.getElementById('main'))"));
		sList.add(new Statement(8000L,2,"if (reporttype === 'sum') {"));
		sList.add(new Statement(9000L,3,"this."+StringUtil.lowerFirst(this.listActiveSumOption.getVerbName())+"(chartstype)"));
		sList.add(new Statement(10000L,3,"myChart.setOption(this.option)"));
		sList.add(new Statement(11000L,2,"} else {"));
		sList.add(new Statement(12000L,3,"this."+StringUtil.lowerFirst(this.listActiveOption.getVerbName())+"(chartstype)"));
		sList.add(new Statement(13000L,3,"myChart.setOption(this.option)"));
		sList.add(new Statement(14000L,2,"}"));
		sList.add(new Statement(15000L,1,"}, 1500)"));
		sList.add(new Statement(16000L,0,"},"));

		method.setMethodStatementList(WriteableUtil.merge(sList));
		return method;
	}

	@Override
	public FrontMethod generateApiMethod() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public FrontCodeBlock generateRouteBlockBlock() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public FrontMethod generateControllerMethod(String indexSuffix) throws Exception {
		return generateControllerMethod();
	}
}
